/*
 *
 * System2 Simulation Framework
 * Copyright (C) 2011 - 2012 Jacob Dawid
 * jacob.dawid@googlemail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "packagemanager.h"
#include "simulationview.h"

#include "guicommunication.h"

#include <QMdiSubWindow>
#include <QFileDialog>

#include "documentpool.h"

namespace System2 {
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow) {
        ui->setupUi(this);

        setWindowIcon(QIcon("icons/s2logo-small.png"));
        new GuiCommunication(ui->consoleTextEdit);


        connect(ui->filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterPackages(QString)));
        connect(ui->consoleLineEdit, SIGNAL(returnPressed()), this, SLOT(executeConsoleCommand()));
        connect(ui->actionCreateNew, SIGNAL(triggered()), this, SLOT(newSimulation()));
        connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openSimulation()));
        connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
        showMaximized();

        preparePackageManager();
    }

    MainWindow::~MainWindow() {
        m_simulator.halt();
        delete ui;
    }

    void MainWindow::filterPackages(QString filter) {
        m_proxyModel.setFilterRegExp(QRegExp(filter, Qt::CaseInsensitive));
        ui->packagesTreeView->expandAll();
    }

    void MainWindow::executeConsoleCommand() {
        QString command = ui->consoleLineEdit->text();
        ui->consoleTextEdit->append("<b>(v8) &lt;&lt;</b> " + command);
        ui->consoleLineEdit->setText("");
        ui->consoleLineEdit->setFocus();
        QString result = m_consoleContext.execute(command);
        ui->consoleTextEdit->append("<b>(v8) &gt;&gt;</b> " + result);
    }


    void MainWindow::newSimulation() {
        SimulationDocument *simulationDocument
                = DocumentPool::instance()->createSimulationDocument();

        QMdiSubWindow *subWindow
                = ui->mdiArea->addSubWindow(
                    new SimulationView(simulationDocument->simulation()));

        subWindow->setWindowTitle("<Unnamed>*");
        subWindow->showMaximized();
    }

    void MainWindow::openSimulation() {
        QString fileName = QFileDialog::getOpenFileName(this, "Open Simulation");
        if(!fileName.isEmpty()) {
            // TODO: Implement.
        }
    }

    void MainWindow::preparePackageManager() {
        PackageManager *pm = PackageManager::instance();
        m_proxyModel.setSourceModel(pm->packagesTreeModel());
        ui->packagesTreeView->setModel(&m_proxyModel);
        pm->loadPackage("../system2-packages/system2-standard/libsystem2-standard.so");
        pm->loadPackage("../system2-packages/system2-photonics/libsystem2-photonics.so");

        ui->packagesTreeView->expandAll();
    }
}
